package com.internetCafes.spms.web.extend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.internetCafes.spms.common.base.BaseWebController;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.core.utils.DateUtils;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.extend.model.EntCertiIsoAudit;
import com.internetCafes.spms.web.extend.service.IEntCertiIsoAuditService;
import com.internetCafes.spms.web.extend.vo.EntCertiIsoAuditVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LinYe
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/cert/entIsoAudit")
public class EntCertiIsoAuditController extends BaseWebController {

    @Autowired
    IEntCertiIsoAuditService entCertiIsoAuditService;

    @SysLog("保存ISO证书年审信息")
    @PostMapping("/save")
    @RequiresPermissions("certificate:certificateInfo:save")
    public R save(@RequestBody EntCertiIsoAudit entity) {
        entCertiIsoAuditService.save(entity);
        return R.ok();
    }

    @SysLog("作废ISO证书年审信息")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("certificate:certificateInfo:delete")
    public R delete(@PathVariable("id") Long id) {
        if (entCertiIsoAuditService.removeById(id)) {
            return R.ok();
        } else {
            return R.error("您要删除的证书不存在");
        }
    }

    @SysLog("更新ISO证书年审信息")
    @PostMapping("/update")
    @RequiresPermissions("certificate:certificateInfo:update")
    public R update(@RequestBody EntCertiIsoAudit entity) {
        if (entCertiIsoAuditService.updateById(entity)) {
            return R.ok();
        } else {
            return R.error("您要更新的证书不存在");
        }
    }

    @GetMapping("/getDetail/{id}")
    @RequiresPermissions("certificate:certificateInfo:info")
    public R get(@PathVariable("id") Long id) {
        EntCertiIsoAudit entCertiIsoAudit = entCertiIsoAuditService.getById(id);
        if (entCertiIsoAudit != null) {
            return R.ok().put("data", entCertiIsoAudit);
        } else {
            return R.error("您要查询的证书不存在");
        }
    }

    @PostMapping("/paging")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R paging(@RequestBody EntCertiIsoAuditVo entCertiIsoAuditVo) {
        Page<EntCertiIsoAudit> page=new Page<>(entCertiIsoAuditVo.getPageNum(),entCertiIsoAuditVo.getPageSize());
        QueryWrapper<EntCertiIsoAudit> entCertiIsoAuditQueryWrapper = new QueryWrapper<>(entCertiIsoAuditVo);
        if (entCertiIsoAuditVo.getOrderBy()!=null) {
            entCertiIsoAuditQueryWrapper.orderBy(entCertiIsoAuditVo.getOrderBy() != null, entCertiIsoAuditVo.isAscFlag(), com.internetCafes.spms.common.utils.StringUtils.HumpToUnderline(entCertiIsoAuditVo.getOrderBy()));
        }
        entCertiIsoAuditQueryWrapper.orderBy(true,false,"id");
        if (entCertiIsoAuditVo.getValidityType()!=null) {
            switch (entCertiIsoAuditVo.getValidityType()) {
                case -1:
                    entCertiIsoAuditQueryWrapper.lt("new_expire_date", new Date());
                    break;
                case 0:
                    entCertiIsoAuditQueryWrapper.gt("new_expire_date", new Date());
                    entCertiIsoAuditQueryWrapper.lt("new_expire_date", entCertiIsoAuditVo.getDays() != null && entCertiIsoAuditVo.getDays() != "" ? DateUtils.getBeAfDay(Integer.parseInt(entCertiIsoAuditVo.getDays())) : entCertiIsoAuditVo.getMonths() != null && entCertiIsoAuditVo.getDays() != "" ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiIsoAuditVo.getMonths())) : null);
                    break;
                case 2:
                    entCertiIsoAuditQueryWrapper.lt("new_expire_date", new Date());
                    entCertiIsoAuditQueryWrapper.or(wrapper -> wrapper.gt("new_expire_date", new Date()).lt("new_expire_date", entCertiIsoAuditVo.getDays() != null ? DateUtils.getBeAfDay(Integer.parseInt(entCertiIsoAuditVo.getDays())) : entCertiIsoAuditVo.getMonths() != null ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiIsoAuditVo.getMonths())) : null));
            }
        }
        IPage<EntCertiIsoAudit> entCertiIsoAuditIPage = entCertiIsoAuditService.page(page,entCertiIsoAuditQueryWrapper);
        return R.ok().put("data", entCertiIsoAuditIPage);
    }

    @PostMapping("/list")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R list(@RequestBody EntCertiIsoAuditVo entCertiIsoAuditVo) {
        QueryWrapper<EntCertiIsoAudit> entCertiIsoAuditQueryWrapper = new QueryWrapper<EntCertiIsoAudit>(entCertiIsoAuditVo);
        entCertiIsoAuditQueryWrapper.orderBy(entCertiIsoAuditVo.getOrderBy()!=null,entCertiIsoAuditVo.isAscFlag(),entCertiIsoAuditVo.getOrderBy());
        List<EntCertiIsoAudit> list = entCertiIsoAuditService.list(entCertiIsoAuditQueryWrapper);
        return R.ok().put("data", list);
    }

    @PostMapping("/count")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R count(@ModelAttribute EntCertiIsoAudit entCertiIsoAudit) {
        QueryWrapper<EntCertiIsoAudit> entCertiIsoAuditQueryWrapper = new QueryWrapper<EntCertiIsoAudit>(entCertiIsoAudit);
        int count = entCertiIsoAuditService.count(entCertiIsoAuditQueryWrapper);
        return R.ok().put("count", count);
    }
}
